package com.briup.srms.project.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.briup.srms.project.bean.Project;
import com.briup.srms.project.bean.ProjectExample;
import com.briup.srms.project.bean.extend.ProjectExtend;
import com.briup.srms.project.dao.ProjectMapper;
import com.briup.srms.project.dao.extend.ProjectExtendMapper;
import com.briup.srms.project.service.IProjectService;
import com.briup.srms.sms.bean.Direction;
import com.briup.srms.sms.service.IDirectionService;
import com.jacky.base.utils.CustomerException;
import com.jacky.base.utils.PageVM;

@Service
public class ProjectServiceImpl implements IProjectService {
	
	@Autowired
	private ProjectMapper projectMapper;
	@Autowired
	private ProjectExtendMapper projectExtendMapper;
	@Autowired
	private IDirectionService directionService;

	@Override
	public PageVM<Project> pageQuery(int page, int pageSize, String name,Long directionId) {
		List<Project> list = projectExtendMapper.query(page, pageSize, name,directionId);
		long total = projectExtendMapper.count(name,directionId);
		return new PageVM<>(page, pageSize, total, list);
	}
	@Override
	public PageVM<ProjectExtend> pageQueryCascade(int page, int pageSize, String name, Long directionId) {
		List<ProjectExtend> list = projectExtendMapper.queryCascade(page, pageSize, name,directionId);
		long total = projectExtendMapper.count(name,directionId);
		return new PageVM<>(page, pageSize, total, list);
	}
	
	@Override
	public void saveOrUpdate(Project project)  throws CustomerException {
		if(project.getId() == null) {
			List<Project> list = this.queryByName(project.getName());
			if(list.size()>0) {
				throw new CustomerException("系统中已经存在同名的毕业设计");
			}
			project.setCreateTime(new Date().getTime());
			project.setStatus(ProjectExtend.STATUS_NO_CHECK);
			projectMapper.insert(project);
		} else {
			projectMapper.updateByPrimaryKey(project);
		}
	}
	
	@Override
	@Transactional
	public void batchInsert(List<Project> projects) throws CustomerException {
		String[] levels_arr = new String[]{"入门","简单","中等","较难","困难"};
		List<String> levels = Arrays.asList(levels_arr);
		
		String[] directions_arr = new String[] {"WebUI","JavaEE","大数据与云计算开发","python"};
		List<String> directions = Arrays.asList(directions_arr);
		
		for(Project project : projects) {
			String level = project.getLevel();
			if(!levels.contains(level)) {
				throw new CustomerException("难度级别输入错误："+level);
			}
			String direction_name = project.getDirectionName();
			if(!directions.contains(direction_name)) {
				throw new CustomerException("方向输入错误："+direction_name);
			}
			Direction direction = directionService.queryByName(direction_name);
			if(direction == null) {
				throw new CustomerException("方向不存在");
			}
			project.setDirectionId(direction.getId());
			this.saveOrUpdate(project);
		}
	}
	
	@Override
	public void deleteById(long id) throws CustomerException {
		Project project = projectMapper.selectByPrimaryKey(id);
		if(project == null) {
			throw new CustomerException("要删除的毕业设计信息不存在");
		}
		projectMapper.deleteByPrimaryKey(id);
	}
	
	private List<Project> queryByName(String name){
		ProjectExample example = new ProjectExample();
		example.createCriteria().andNameEqualTo(name);
		return projectMapper.selectByExample(example);
	}

}
